///**
// * 
// */
//package com.leyu.mall.site.shiro.ext;
//
//import java.io.Serializable;
//
//import javax.annotation.PostConstruct;
//import javax.annotation.Resource;
//
//import org.apache.commons.lang3.builder.EqualsBuilder;
//import org.apache.commons.lang3.builder.HashCodeBuilder;
//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.AuthenticationInfo;
//import org.apache.shiro.authc.AuthenticationToken;
//import org.apache.shiro.authc.DisabledAccountException;
//import org.apache.shiro.authc.SimpleAuthenticationInfo;
//import org.apache.shiro.authc.UsernamePasswordToken;
//import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.apache.shiro.util.ByteSource;
//import org.springframework.stereotype.Service;  
//
//import com.leyu.mall.common.utils.Encodes;
//import com.leyu.mall.domain.member.Member;
//import com.leyu.mall.service.member.MemberService;
//
///**
// * @author change
// * 
// */
//@Service
//public class SiteAuthorizingRealm extends AuthorizingRealm {
//	public static final String HASH_ALGORITHM = "SHA-1";
//	public static final int HASH_INTERATIONS = 1024; 
//	@Resource
//	private MemberService memberService;
//
//	/*
//	 * (non-Javadoc)
//	 * 
//	 * @see
//	 * org.apache.shiro.realm.AuthorizingRealm#doGetAuthorizationInfo(org.apache
//	 * .shiro.subject.PrincipalCollection)
//	 */
//	@Override
//	protected AuthorizationInfo doGetAuthorizationInfo(
//			PrincipalCollection principal) {
//		MemberModel memberModel = (MemberModel) principal.getPrimaryPrincipal();
//		Member member = memberService.findMemberByLoginName(memberModel
//				.getUsername());
//		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//		/*info.addRoles(user.getRoleList());*/
//		return info;
//	}
//
//	/*
//	 * (non-Javadoc)
//	 * 
//	 * @see
//	 * org.apache.shiro.realm.AuthenticatingRealm#doGetAuthenticationInfo(org
//	 * .apache.shiro.authc.AuthenticationToken)
//	 */
//	@Override
//	protected AuthenticationInfo doGetAuthenticationInfo(
//			AuthenticationToken authcToken) throws AuthenticationException {
//		UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
//		Member member = memberService
//				.findMemberByLoginName(token.getUsername());
//		if (member != null) {
//			if ("disabled".equals(member.getStatus())) {
//				throw new DisabledAccountException();
//			}
//
//			byte[] salt = Encodes.decodeHex(member.getSalt());
//			return new SimpleAuthenticationInfo(this.createMemberModel(member),
//					member.getPassword(), ByteSource.Util.bytes(salt),
//					getName());
//		} else {
//			return null;
//		}
//	}
//
//	private MemberModel createMemberModel(Member member) {
//		MemberModel memberModel = new MemberModel();
//		memberModel.id=member.getId();
//		memberModel.username=member.getUsername();
//		memberModel.email=member.getEmail();
//		memberModel.mobile=member.getMobile(); 
//		return memberModel;
//
//	}
//	/**
//	 * 设定Password校验的Hash算法与迭代次数.
//	 */
//	@PostConstruct
//	public void initCredentialsMatcher() {
//		HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(HASH_ALGORITHM);
//		matcher.setHashIterations(HASH_INTERATIONS);
//
//		setCredentialsMatcher(matcher);
//	}
//	/**
//	 * 自定义Authentication对象，使得Subject除了携带用户的登录名外还可以携带更多信息.
//	 */
//	public static class MemberModel implements Serializable {
//		private static final long serialVersionUID = -1373760761780840081L;
//		Long id;
//		String username;
//		String email;
//		String mobile;
//		String address;
//		String sex;
//
//		public Long getId() {
//			return id;
//		}
// 
//
//		public String getUsername() {
//			return username;
//		}
//
//		 
//		public String getEmail() {
//			return email;
//		}
//
//		 
//		public String getMobile() {
//			return mobile;
//		}
//
//		 
//		public String getAddress() {
//			return address;
//		}
// 
//
//		public String getSex() {
//			return sex;
//		}
//
// 
//
//
//		/**
//		 * 本函数输出将作为默认的<shiro:principal/>输出.
//		 */
//		@Override
//		public String toString() {
//			return username;
//		}
//
//		/**
//		 * 重载equals,只计算loginName;
//		 */
//		@Override
//		public int hashCode() {
//			return HashCodeBuilder.reflectionHashCode(this, "username");
//		}
//
//		/**
//		 * 重载equals,只比较loginName
//		 */
//		@Override
//		public boolean equals(Object obj) {
//			return EqualsBuilder.reflectionEquals(this, obj, "username");
//		}
//	}
//}
